package com.yanyeori.framework.datasource.strategy;

import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Random;

/**
 * 集合策略-随机
 *
 * @author chenkuan 2021/8/18
 */
public class ListRandomStrategy implements ListStrategy {

    private final Random random = new Random();

    @Override
    public <T> T get(List<T> list, Object arg) {
        if (CollectionUtils.isEmpty(list)) return null;
        if (list.size() == 1) return list.get(0);
        return list.get(random.nextInt(list.size()));
    }
}
